package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.ProjPlanLogic;
import cn.topevery.garden.entity.dto.ProjPlanCudDTO;
import cn.topevery.garden.entity.dto.ProjPlanDTO;
import cn.topevery.garden.entity.query.ProjBidQuery;
import cn.topevery.garden.entity.query.ProjPlanQuery;
import cn.topevery.garden.entity.vo.ProjBidAndProjNameVO;
import cn.topevery.garden.entity.vo.ProjPlanAndFilesVO;
import cn.topevery.garden.entity.vo.ProjPlanAndProjNameVO;
import cn.topevery.garden.entity.vo.ProjPlanVO;
import cn.topevery.garden.framework.util.CurrentUserUtil;
import cn.topevery.garden.framework.Database;
import hw.topevery.framework.DataFillHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 项目计划表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Api(tags = "工程项目-项目计划表")
@RestController
@RequestMapping("/projPlan")
public class ProjPlanController extends BaseController{


    @Autowired
    ProjPlanLogic projPlanLogic;

    /**
     * 获取项目计划表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取项目计划表页列表")
    @GetMapping("/listPage")
    public JsonResult<DataPager<ProjPlanVO>> listPage(ProjPlanQuery query) {
        return JsonResult.ok(projPlanLogic.listPage(query));
    }

    @ApiOperation(value = "获取项目招标表列表接口")
    @PostMapping("/list")
    public JsonResult<List<ProjPlanAndProjNameVO>> list(@RequestBody ProjPlanQuery query) {
        return JsonResult.ok(projPlanLogic.list(query));
    }

    @ApiOperation("新增/编辑 项目计划表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody List<ProjPlanDTO> dtoList) {
        Database.transactionScope(() -> {
            projPlanLogic.saveOrUpdate(getUser().userId, dtoList);
        });
        return JsonResult.ok(true);
    }

    @ApiOperation("新增/编辑/删除 项目计划表信息")
    @PostMapping("/saveOrUpdateOrDelete")
    public JsonResult<Boolean> saveOrUpdateOrDelete(@Valid @RequestBody ProjPlanCudDTO cudDto) {
        Database.transactionScope(() -> {
            projPlanLogic.saveOrUpdateOrDelete(getUser().userId, cudDto);
        });
        return JsonResult.ok(true);
    }

    @ApiOperation("根据guid获取详情")
    @GetMapping("/getByGuid")
    public JsonResult<ProjPlanVO> getByGuid(@RequestParam(value = "guid") String guid) {
        return JsonResult.ok(projPlanLogic.getEntityById(guid));
    }

    @ApiOperation("根据guid获取详情")
    @GetMapping("/getDetailAndFileByGuid")
    public JsonResult<ProjPlanAndFilesVO> getDetailAndFileByGuid(@RequestParam(value = "guid") String guid) {
        return JsonResult.ok(projPlanLogic.getDetailAndFileByGuid(guid));
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        projPlanLogic.deleteBatchById(CurrentUserUtil.getUser().userId,ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出项目计划表信息")
    @GetMapping("/export")
    public void export(ProjPlanQuery query, HttpServletResponse response) {
        List<ProjPlanVO> list = projPlanLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "项目计划表", "sheet1", ProjPlanVO.class, "项目计划表", response);
    }

}
